package com.shuwen.gcdj.controller.api;

import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.bean.AddResp;
import com.shuwen.gcdj.bean.VoidResp;
import com.shuwen.gcdj.bean.api.request.AddApiReq;
import com.shuwen.gcdj.bean.api.request.GetApiListReq;
import com.shuwen.gcdj.bean.api.request.UpdateApiReq;
import com.shuwen.gcdj.bean.api.response.ApiItemResp;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.common.enums.ErrorCode;
import com.shuwen.gcdj.common.enums.LogModuleEnum;
import com.shuwen.gcdj.common.enums.LogTypeEnum;
import com.shuwen.gcdj.common.error.ErrorReport;
import com.shuwen.gcdj.common.sysLog.AddSysLogUtil;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.dto.account.InitUserDto;
import com.shuwen.gcdj.service.api.ApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户中心 - Api管理")
@RestController
@RequestMapping("/api")
@Slf4j
public class ApiController extends AuthController {

    @Autowired
    private ApiService apiService;

    @Autowired
    private AddSysLogUtil addSysLogUtil;

    @ApiOperation(value = "获取所有Api")
    @PostMapping(value = "/getList")
    @ResponseBody
    @JWTAuth(apiCode = "USER_API_API_GETLIST")
    public CommonResult<List<ApiItemResp>> getList(HttpServletRequest request, @Valid @RequestBody GetApiListReq req) {
        return CommonResult.success(apiService.searchList(req));
    }

    @ApiOperation(value = "添加Api")
    @PostMapping(value = "/add")
    @ResponseBody
    @JWTAuth(apiCode = "USER_API_API_ADD")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<AddResp> add(HttpServletRequest request, @Valid @RequestBody AddApiReq req) {
        return CommonResult.success(apiService.createApi(req, initUser(request)));
    }

    @ApiOperation(value = "修改Api")
    @PostMapping(value = "/update")
    @ResponseBody
    @JWTAuth(apiCode = "USER_API_API_UPDATE")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> update(HttpServletRequest request, @Valid @RequestBody UpdateApiReq req) {
        InitUserDto initUserDto = initUser(request);
        apiService.updateApi(req, initUserDto);
        addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_UPDATE.getTypeNum(), LogModuleEnum.LOG_MODULE_API.getModuleNum(), "修改了API",req);
        return CommonResult.success(new VoidResp());
    }

    /**
     * 删除Api
     *
     * @param apiId String
     * @return CommonResult<VoidResp>
     */
    @ApiOperation(value = "批量删除Api")
    @GetMapping(value = "/delete/{apiId}")
    @ResponseBody
    @JWTAuth(apiCode = "USER_API_API_DELETE")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> delete(HttpServletRequest request, @Valid @PathVariable String apiId) {
        ErrorCode res = apiService.deleteApi(apiId, initUser(request));
        if (ErrorReport.hasError(res)) {
            return CommonResult.failed(res);
        }

        return CommonResult.success(new VoidResp());
    }
}
